package ej1.Main;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Strain {

	private int _id;
	private Collection<Strain> _mutations;
	private boolean _isGettingLongestMutationPath;
	private MutationPath _longestMutationPathCache;
	public static int PASSES = 0;

	public Strain(int aStrainId) {
		_id = aStrainId;
		_mutations = new ArrayList<Strain>();
	}

	public void nowCanMutateTo(Strain anotherStrain) {
		_mutations.add(anotherStrain);
	}

	public int getId() {
		return _id;
	}

	public Iterable<Strain> getMutations() {
		
		return (Iterable<Strain>) _mutations;
	}

	public MutationPath getLongestMutationPathComingFrom(Strain aStrain) {
		
		PASSES ++;
		
		if (_longestMutationPathCache != null)
			return _longestMutationPathCache;
		
		if (_isGettingLongestMutationPath)
			return _longestMutationPathCache = new CiclicMutationPath();
		else
			_isGettingLongestMutationPath = true;
		
		MutationPath longestMutationPath = new MutationPath();
		
		Iterator<Strain> i = _mutations.iterator();
		Strain actualStrain;
		
		while(i.hasNext()){
			
			actualStrain = i.next();
			
			if (actualStrain.getLongestMutationPathComingFrom(aStrain).isLongerThan(longestMutationPath))
				longestMutationPath = actualStrain.getLongestMutationPathCache();
			
		}
		
		_isGettingLongestMutationPath = false;
		
		_longestMutationPathCache = longestMutationPath.clone().precededBy(this);
		
		if (_longestMutationPathCache == null)
			System.out.println("frutaaa");
		
		return _longestMutationPathCache;
	}

	public MutationPath getLongestMutationPathCache() {
		// TODO Auto-generated method stub
		return _longestMutationPathCache;
	}		

}
